package com.lc.afbx.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lc.afbx.BaseApplication;
import com.lc.afbx.R;
import com.lc.afbx.conn.GetApiShowTiXian;
import com.lc.afbx.conn.PostApiWithdraw;
import com.lc.afbx.util.MyToast;
import com.lc.afbx.util.PrefrenceConfig;
import com.lc.afbx.util.Validator;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/17.
 */
public class CommissionWithdrawActivity extends BaseActivity implements View.OnClickListener {

    private TextView commission_tixian_money_tv;
    //    private Button commission_tixian_bt;
    private TextView commission_tixian_bank_name_tv;
    private EditText commission_tixian_bank_num_et;

    private List<GetApiShowTiXian.Bank> dataList;
    private List<String> list;

    private PostApiWithdraw postApiWithdraw = new PostApiWithdraw(new AsyCallBack<PostApiWithdraw.Info>() {
        @Override
        public void onSuccess(String toast, int type, PostApiWithdraw.Info info) throws Exception {

            UtilToast.show(context, "申请成功");

            CommissionCanWithdrawActivity.MyNotifyCallback callback = (CommissionCanWithdrawActivity.MyNotifyCallback) getAppCallBack(CommissionCanWithdrawActivity.class);
            callback.Refrush();

            CommissionActivity.RefreshCommissionCallback refreshCommissionCallback = (CommissionActivity.RefreshCommissionCallback) getAppCallBack(CommissionActivity.class);
            refreshCommissionCallback.refreshCommission();

            finish();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(context, toast);
        }
    });

    private GetApiShowTiXian getApiShowTiXian = new GetApiShowTiXian(new AsyCallBack<GetApiShowTiXian.Info>() {
        @Override
        public void onSuccess(String toast, int type, GetApiShowTiXian.Info info) throws Exception {
            dataList = info.bankList;
            commission_tixian_money_tv.setText(info.data.commission);//可提现金额 设置数据
            commission_tixian_bank_name_tv.setText(dataList.get(0).title);//给pop设置默认的银行  datalist的 第一条

            list = new ArrayList<>();
            for (int i = 0; i < dataList.size(); i++) {
                list.add(dataList.get(i).title);
            }

        }
    });


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_commission_tixian);

        commission_tixian_bank_name_tv = ((TextView) findViewById(R.id.commission_tixian_bank_name_tv));
        commission_tixian_money_tv = ((TextView) findViewById(R.id.commission_tixian_money_tv));
//        commission_tixian_bt = ((Button) findViewById(R.id.commission_tixian_bt));
        commission_tixian_bank_num_et = ((EditText) findViewById(R.id.commission_tixian_bank_num_et));

        getApiShowTiXian.userid = BaseApplication.BasePreferences.readUserId();
        getApiShowTiXian.execute(context);

        commission_tixian_bank_name_tv.setOnClickListener(this);

    }


    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.back:
                finish();
                return;

            case R.id.commission_tixian_bank_name_tv:
                showPopupWindow(view);
                return;

            case R.id.commission_tixian_bt:

                if (TextUtils.isEmpty(commission_tixian_bank_name_tv.getText().toString())) {
                    MyToast.showToast("请选择银行", context);
                    return;
                }
                if (TextUtils.isEmpty(commission_tixian_bank_num_et.getText().toString())) {
                    MyToast.showToast("请填写银行卡号", context);
                    return;
                }
                if (!Validator.isBankNum(commission_tixian_bank_num_et.getText().toString())) {
                    MyToast.showToast("请填写正确银行卡号", context);
                    return;
                }
                if (!(getIntent().getStringExtra(PrefrenceConfig.INTENT_TAG).equals(commission_tixian_money_tv.getText().toString()))) {
                    MyToast.showToast("上个页面带过来的 和这个页面请求的总钱数 不一样", context);
                    return;
                }

                postApiWithdraw.commission = commission_tixian_money_tv.getText().toString();
                postApiWithdraw.bankname = commission_tixian_bank_name_tv.getText().toString();
                postApiWithdraw.userid = BaseApplication.BasePreferences.readUserId();
                postApiWithdraw.banknum = commission_tixian_bank_num_et.getText().toString();
                postApiWithdraw.withdraw_id = getIntent().getStringExtra("ids");

                postApiWithdraw.execute(context);
                return;


        }

    }

    private void showPopupWindow(View view) {

        View contentView = LayoutInflater.from(context).inflate(R.layout.pop_layout, null);

        ListView listView = ((ListView) contentView.findViewById(R.id.item_popList));

        listView.setAdapter(new ArrayAdapter(context, R.layout.support_simple_spinner_dropdown_item, list));

        final PopupWindow popupWindow = new PopupWindow(contentView, commission_tixian_bank_name_tv.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT, true);

        popupWindow.setTouchable(true);
        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });


        popupWindow.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.shape_white_bg));

        popupWindow.showAsDropDown(view);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                commission_tixian_bank_name_tv.setText(dataList.get(position).title.toString());
                popupWindow.dismiss();
            }
        });

    }


}
